/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wiView.graphics.Drawer.GraphAnimation;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hoang
 */
public abstract class AbstractAnimationHandle implements IAnimationHandle, Runnable{
/**
 * lock indicate that whether paint thread's running
 */
    boolean lock = false;
    /**
     * time in ms to stop image to show to human's eyes
     */
    protected int paintDelay = 45;
    /**
     * time in ms to finish adding Animator
     */
    protected int waitTime = 1;
    /**
     * Thread to run painting
     */
    protected Thread paintThread;
    public AbstractAnimationHandle() {
    }
    /**
     * To start paint thread
     */
    public void Start(){
        paintThread = new Thread(this);
        paintThread.start();
    }
    /**
     * implement Runnable
     */
    @Override
    public void run() {
        while (true) {
            if (!canRun()) {
                try {
                    Thread.sleep(waitTime);
                } catch (InterruptedException ex) {
                    System.out.println(ex.getMessage());
                } finally {
                    continue;
                }
            }
            try {
                Thread.sleep(paintDelay);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
                continue;
            }
            lock = true;
            try {
                Thread.sleep(waitTime);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
                continue;
            }
            Repaint();
            lock = false;
        }
    }
    /**
     * implement IAnimatorHandle
     * @param as list of Animator
     */
    @Override
    public void addAnimator(ArrayList<Animator> as) {
        if(as == null){
            //System.out.println("Animation handle: wrong event");
            return;
        }
        if (as.size() == 0) {
            //System.out.println("Animation handle: event not in range");
            return;
        }

        
        for (Animator ani : as) {
            InitBeforeAdding(ani);
        while(lock){
            try {
                Thread.sleep(1);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
                continue;
            }
        }
        _addAnimator(ani);
        }
        
    }
/**
 * Add more information to ani before add to Animator Queue
 * @param ani Animator to add to Queue
 */
    protected abstract void InitBeforeAdding(Animator ani);
/**
 * add Animator to Queue here
 * @param ani - Animator to add
 */
    protected abstract void _addAnimator(Animator ani);

    /**
     * Redraw network image
     */
    protected abstract void Repaint();
/**
 * check if Repaint method should run
 * @return true if Repaint should run
 */
    protected abstract boolean canRun();
    /**
     * indicate that Repaint method continue or break
     * @return 
     */
    protected abstract boolean isContinue();
    /**
     * Stop paint thread
     */
    public void Stop(){
        
    }
}
